/*
 * Filename:    SelfFeedbackDetailController.java
 * Copyright:   2014 中国平安保险（集团）股份有限公司 版权所有
 * Company:     平安科技养老险及健康险系统开发部
 * Create at:   2014年11月14日
 */
package com.paic.mhis.hcpms.warning.web.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.paic.mhis.core.auth.biz.service.UserDetails;
import com.paic.mhis.hcpms.agreement.healthcareprovider.util.HttpProcessUtil;
import com.paic.mhis.hcpms.common.document.dto.DocContentDTO;
import com.paic.mhis.hcpms.common.document.dto.DocParameterCondition;
import com.paic.mhis.hcpms.common.workflow.vo.HcpmsWorkFlowVO;
import com.paic.mhis.hcpms.warning.biz.service.HcpmsWarnService;
import com.paic.mhis.hcpms.warning.biz.service.SelfFeedbackDetailService;
import com.paic.mhis.hcpms.warning.biz.service.WarningWorkFlowService;
import com.paic.mhis.hcpms.warning.dto.HcpmsWarningDTO;
import com.paic.mhis.hcpms.warning.dto.HcpmsWarnsDTO;
import com.paic.mhis.hcpms.warning.dto.WarningCheckDTO;
import com.paic.mhis.hcpms.warning.dto.WarningCheckDetailDTO;
import com.paic.mhis.hcpms.warning.util.WarnConstants;
import com.paic.pafa.appclient.annotation.BizServicesProxy;

/**
 * 医院自查信息
 */
@Controller
@RequestMapping("/hcpmsWarning/")
public class SelfFeedbackDetailController {
	@BizServicesProxy
	private SelfFeedbackDetailService selfFeedbackDetailService;
	@BizServicesProxy
	private HcpmsWarnService hcpmsWarnService;
	@BizServicesProxy
	private WarningWorkFlowService warningWorkFlowService;

	/**
	 * 预警提醒查看
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping("getSelfFeedBackWarn.do")
	@ResponseBody
	public Map<String, Object> getSelfFeedBackWarn(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		String warnNo = request.getParameter("warnNo");
		HcpmsWarnsDTO warninfo = hcpmsWarnService.getWarnInfo(warnNo);
		map.put("warninfo", warninfo);
		return map;
	}

	/**
	 * 预警提醒确认
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping("getSelfFeedBackWarnCheck.do")
	@ResponseBody
	public Map<String, Object> getSelfFeedBackWarnCheck(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		UserDetails user = (UserDetails) request.getSession().getAttribute("user");
		try {
			String warnNo = request.getParameter("warnNo");
			HcpmsWorkFlowVO wfvo = new HcpmsWorkFlowVO();
			wfvo.setTaskNo(warnNo);
			wfvo.setActCurrent(user.getUsername());
			HcpmsWarnsDTO warnInfo = selfFeedbackDetailService.getWarnInfo(warnNo);
			wfvo.setAssigner(warnInfo.getAgent());
			Map<String, String> condition = new HashMap<String, String>();
			condition.put("PROCESS_TYPE", warnInfo.getProcess());
			wfvo.setParamValue(condition);
			// 启动
			String actCurrent = warningWorkFlowService.processWorkFlow(wfvo);
			// 更新状态
			HcpmsWarningDTO warnDto = new HcpmsWarningDTO();
			warnDto.setWarnNo(warnNo);
			warnDto.setStatus(actCurrent);
			warnDto.setIsFeedback(WarnConstants.WARN_ISFEEDBACK_02);
			hcpmsWarnService.updateHcpmsWarn(warnDto);

			map.put("result", true);
			map.put("msg", "操作成功");
		} catch (Exception e) {
			map.put("result", false);
			map.put("msg", "操作失败");
		}
		return map;
	}

	/**
	 * 医院自查整改查看
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping("getSelfFeedBackList.do")
	@ResponseBody
	public Map<String, Object> getFeedBackList(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		Map<String, Object> paramMap = new HashMap<String, Object>();
		String warnNo = request.getParameter("warnNo");
		paramMap.put("warnno", warnNo);
		List<WarningCheckDetailDTO> warningCheckDetailList = selfFeedbackDetailService.getFeedBackDetailList(paramMap);
		List<WarningCheckDTO> warningCheckList = selfFeedbackDetailService.getFeedBackList(paramMap);
		DocParameterCondition param = new DocParameterCondition();
		param.setTaskNo(warnNo);
		param.setTaskType(WarnConstants.TASK_TYPE_WARNING);
		param.setDocType(WarnConstants.DOC_TYPE_NOTICE);
		List<DocContentDTO> docContentList = selfFeedbackDetailService.queryByDocParameterCondition(param);
		if (warningCheckDetailList.size() > 0) {
			map.put("feedbackDetail", warningCheckDetailList);
		}
		if (warningCheckList.size() > 0) {
			map.put("feedback", warningCheckList.get(0));
		}
		if (docContentList.size() > 0) {
			map.put("notice", docContentList);
		}
		return map;
	}

	/**
	 * 医院自查整改反馈保存
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "saveSelfFeedBack.do", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> saveSelfFeedBack(HttpServletRequest request) throws Exception{
		Map<String, Object> map = new HashMap<String, Object>();
		UserDetails user = (UserDetails) request.getSession().getAttribute("user");
		try {
			String str = HttpProcessUtil.readRequestBody(request);
			JSONObject transferJson = JSON.parseObject(str);
			int result = selfFeedbackDetailService.saveSelfFeedBack(transferJson);
			if (result > 0) {
				// 获取参数
				Map<String, Object> param = JSONObject.toJavaObject(transferJson, Map.class);
				String warnNo = (String) param.get("warnNo");
				HcpmsWorkFlowVO wfvo = new HcpmsWorkFlowVO();
				wfvo.setTaskNo(warnNo);
				wfvo.setActCurrent(user.getUsername());
				HcpmsWarnsDTO warnInfo = selfFeedbackDetailService.getWarnInfo(warnNo);
				wfvo.setAssigner(warnInfo.getAgent());
				// 启动
				String actCurrent = warningWorkFlowService.processWorkFlow(wfvo);
				// 更新状态
				HcpmsWarningDTO warnDto = new HcpmsWarningDTO();
				warnDto.setWarnNo(warnNo);
				warnDto.setStatus(actCurrent);
				warnDto.setIsFeedback(WarnConstants.WARN_ISFEEDBACK_02);
				warnDto.setFeedbackDate(new Date());
				warnDto.setIsCheckBack(WarnConstants.WARN_ISCHECKBACK_01);
				hcpmsWarnService.updateHcpmsWarn(warnDto);

				map.put("result", true);
				map.put("msg", "操作成功");

			} else {
				map.put("result", false);
				map.put("msg", "操作失败");
			}
		} catch (Exception e) {
			map.put("result", false);
			map.put("msg", "操作失败");
		}
		return map;
	}

	/**
	 * 医院自查整改反馈查询
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "getSelfFeedBackNotice.do", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> getSelfFeedBackNotice(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		String warnNo = request.getParameter("warnNo");
		DocParameterCondition param = new DocParameterCondition();
		param.setTaskNo(warnNo);
		param.setTaskType(WarnConstants.TASK_TYPE_WARNING);
		param.setDocType(WarnConstants.DOC_TYPE_NOTICE);
		try {
			List<DocContentDTO> docContentList = selfFeedbackDetailService.queryByDocParameterCondition(param);
			if (docContentList.size() > 0) {
				map.put("selfFeedback", docContentList);
				map.put("warnNo", warnNo);
				map.put("result", true);
				map.put("msg", "查询成功");
			} else {
				map.put("result", false);
				map.put("msg", "查询数据有误");
			}
		} catch (Exception e) {
			map.put("result", false);
			map.put("msg", "查询失败");
		}
		return map;
	}

}
